package com.molyfun.parents.view;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.molyfun.parents.R;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * Created by william(liuchao.hit@gmail.com).
 * Date:10/18/16.
 * 底部横条的双Tab切换tab
 */
public class TwoBaselineTabBar extends LinearLayout implements View.OnClickListener{

    private int mChooseTab = TwoBaslineTabChooseTab.BAR_ONE;

    public static interface TwoBaslineTabChooseTab{
        public static final int BAR_ONE = 1001;
        public static final int BAR_TWO = 1002;
    }

    protected LayoutInflater layoutInflater;

    private TwoBaselineBarClickListener listener;
    private Context context;
    private boolean inited;

    @InjectView(R.id.tab_one)
    TextView mTabOne;
    @InjectView(R.id.tab_two)
    TextView mTabTwo;

    public TwoBaselineTabBar(Context context, AttributeSet attrs, int defStyle) {
        this(context, attrs);
    }

    public TwoBaselineTabBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public TwoBaselineTabBar(Context context) {
        super(context);
        init(context, null);
    }

    private void init(Context context, AttributeSet attrs) {
        this.context = context;
        layoutInflater = LayoutInflater.from(context);
        layoutInflater.inflate(R.layout.widget_two_baseline_tab_choose_bar, this);
        ButterKnife.inject(this);
        mTabOne.setOnClickListener(this);
        mTabTwo.setOnClickListener(this);
    }

    public int getCurrentChooseTab(){
        return mChooseTab;
    }

    /**
     * init view
     *
     */
    public void initData(String tab1, String tab2) {
        if(inited){
            return;
        }
        mTabOne.setText(tab1);
        mTabTwo.setText(tab2);
        inited = true;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tab_one:
                if(mChooseTab != TwoBaslineTabChooseTab.BAR_ONE){
                    setTabClick(TwoBaslineTabChooseTab.BAR_ONE);
                    if(listener != null){
                        listener.onTwoBaseLineBarClick(TwoBaslineTabChooseTab.BAR_ONE);
                    }
                }
                break;
            case R.id.tab_two:
                if(mChooseTab != TwoBaslineTabChooseTab.BAR_TWO){
                    setTabClick(TwoBaslineTabChooseTab.BAR_TWO);
                    if(listener != null){
                        listener.onTwoBaseLineBarClick(TwoBaslineTabChooseTab.BAR_TWO);
                    }
                }
                break;
        }
    }

    /**
     * only view
     * @param tab
     */
    public void setTabClick(int tab){
        if(tab == TwoBaslineTabChooseTab.BAR_ONE){
            mTabOne.setBackgroundResource(R.drawable.bg_baseline_tabbar_clicked);
            mTabOne.setTextColor(ContextCompat.getColor(context, R.color.color_base_title_bar));
            mTabTwo.setBackgroundColor(ContextCompat.getColor(context, R.color.color_white));
            mTabTwo.setTextColor(ContextCompat.getColor(context, R.color.text_color_normal));
            mChooseTab = TwoBaslineTabChooseTab.BAR_ONE;
        }else{
            mTabOne.setBackgroundColor(ContextCompat.getColor(context, R.color.color_white));
            mTabOne.setTextColor(ContextCompat.getColor(context, R.color.text_color_normal));
            mTabTwo.setBackgroundResource(R.drawable.bg_baseline_tabbar_clicked);
            mTabTwo.setTextColor(ContextCompat.getColor(context, R.color.color_base_title_bar));
            mChooseTab = TwoBaslineTabChooseTab.BAR_TWO;
        }
    }

    public void setTwoBaseLineBarClickListener(TwoBaselineBarClickListener listener) {
        this.listener = listener;
    }

    public static interface TwoBaselineBarClickListener {
        void onTwoBaseLineBarClick(int chooseTab);
    }

}